package com.mammoth.Bodybuilding.service.Impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.mammoth.Bodybuilding.entity.ResultObj;
import com.mammoth.Bodybuilding.entity.SysMenu;
import com.mammoth.Bodybuilding.entity.SysRole;
import com.mammoth.Bodybuilding.respository.IMenuRepository;
import com.mammoth.Bodybuilding.respository.IRoleRepository;
import com.mammoth.Bodybuilding.service.IMenuService;

/**
 * 
 * 
 * @title : MenuServiceImpl.java
 * @description : 菜单 服务实现类
 * @company : com.mammoth.Bodybuilding.service.Impl
 * @project Mammoth
 * @author xingzhaojun
 * @date 2018年4月27日上午10:58:48
 */
@Service
@Transactional
public class MenuServiceImpl implements IMenuService {

	/** 注入菜单数据仓库 **/
	@Autowired
	private IMenuRepository menuRespository;
	
	/** 自动注入role数据仓库 **/
	@Autowired
	private IRoleRepository roleRepository;

	/**
	 * 根据fid 以及权限获取菜单
	 * 
	 * @param fid
	 *            父id
	 * @param role
	 *            权限对象
	 * @return
	 */
	@Override
	public List<SysMenu> getMenuByFidAndRole(long fid, SysRole role) {
		List<SysMenu> sysmenus = menuRespository.findAllByFid(fid);
		List<SysMenu> menu = new ArrayList<>();
		/** 过滤权限 **/
		for (SysMenu sysMenu : sysmenus) {
			if (sysMenu.getRole().contains(role)) {
				menu.add(sysMenu);
			}
		}
		return menu;
	}

	/**
	 * 根据路径获取菜单
	 */
	@Override
	public SysMenu getMenuByPath(String path) {
		return menuRespository.findMenuByPath(path);
	}

	/**
	 * 获取所有一级菜单
	 */
	@Override
	public List<SysMenu> getFMenu() {
		return menuRespository.findAllByFid((long) 0);
	}

	/**
	 * 分页查询所有菜单信息
	 */
	@Override
	public ResultObj getAllMenu(int pageSize, int pageIndex, String name, String path, Long fid) {
		ResultObj resultObj = new ResultObj();
		Pageable pageable = new QPageRequest(pageIndex - 1, pageSize);
		Page<SysMenu> page = null;
		if (fid == 0) {
			page = menuRespository.findMenuByParam(pageable, name, path);
		} else {
			page = menuRespository.findMenuByParam(pageable, name, path, fid);
		}
		resultObj.setCode(1);
		resultObj.setFlag(true);
		resultObj.setTotal(page.getTotalElements());
		resultObj.setResult(page.getContent());
		resultObj.setMessage("查询成功");
		return resultObj;
	}

	@Override
	public String getByFid(long fid) {
		return menuRespository.findByFid(fid);
	}

	@Override
	public ResultObj saveMenu(String menuName, String menuPath, String menuIcon, Long fMenu, List<Long> role,
			int sore) {
		ResultObj resultObj = new ResultObj();
		List<SysRole> roles = new ArrayList<>();
		for (Long index : role) {
			Optional<SysRole> tempRole = roleRepository.findById(index);
			if(tempRole.isPresent()) {
				roles.add(tempRole.get());
			}else {
				continue;
			}
		}
		SysMenu menu = new SysMenu();
		menu.setMenuName(menuName);
		menu.setMenuPath(menuPath);
		menu.setMenuFa(menuIcon);
		menu.setFid(fMenu);
		menu.setRole(roles);
		menu.setSortNum(sore);
		menuRespository.save(menu);
		resultObj.setCode(1);
		resultObj.setFlag(true);
		resultObj.setMessage("保存菜单成功");
		return resultObj;
	}

	@Override
	public ResultObj delMenu(List<Long> id) {
		ResultObj resultObj = new ResultObj();
		for (Long index : id) {
			menuRespository.deleteById(index);
		}
		resultObj.setCode(1);
		resultObj.setFlag(true);
		resultObj.setMessage("菜单删除成功");
		return resultObj;
	}

	@Override
	public ResultObj updateMenu(Long id, String menuName, String menuPath, String menuIcon, Long fMenu, List<Long> role,
			int sore) {
		ResultObj resultObj = new ResultObj();
		List<SysRole> roles = new ArrayList<>();
		for (Long index : role) {
			Optional<SysRole> tempRole = roleRepository.findById(index);
			if(tempRole.isPresent()) {
				roles.add(tempRole.get());
			}else {
				continue;
			}
		}
		Optional<SysMenu> tempMenu = menuRespository.findById(id);
		if(tempMenu.isPresent()) {
			SysMenu sysMenu = tempMenu.get();
			sysMenu.setMenuName(menuName);
			sysMenu.setMenuPath(menuPath);
			sysMenu.setMenuFa(menuIcon);
			sysMenu.setFid(fMenu);
			sysMenu.setRole(roles);
			sysMenu.setSortNum(sore);
			menuRespository.save(sysMenu);
			resultObj.setCode(1);
			resultObj.setFlag(true);
			resultObj.setMessage("修改菜单成功");
		}else {
			resultObj.setCode(1);
			resultObj.setFlag(false);
			resultObj.setMessage("菜单不存在，修改失败");
		}
		return resultObj;
	}

}
